package com.zhongke.common.imageloader

import android.widget.ImageView
import androidx.annotation.Dimension
import coil.Coil
import coil.ImageLoader
import coil.imageLoader
import coil.load
import coil.request.Disposable
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import com.zhongke.common.base.application.ZKBaseApplication

/**
 *@Date: 2022/5/9
 *@Time: 17:44
 *@Author:cxc
 *@Description:
 */
object ZKCoilDefaultImageLoader  {

    init {
        Coil.setImageLoader(
            ImageLoader.Builder(ZKBaseApplication.getContext())
                .crossfade(true)
                // .placeholder()
                // .error()
                .build()
        )
    }

   inline fun loadImage(
        imageView: ImageView, data: Any?,
        @Dimension(unit = Dimension.DP) radius: Int = 0,
        @ImageType imageType: Int = ImageType.NORMAL,
        @CornerType type: Int = CornerType.ALL,
        imageLoader: ImageLoader = imageView.context.imageLoader,
        options: ImageRequest.Builder.() -> Unit = {}
    ): Disposable {
        return when (imageType) {
            ImageType.ROUND -> {
                realLoadImage(imageView, data, imageLoader) {
                    transformations(CircleCropTransformation())
                    options()
                }
            }
            ImageType.CORNER ->{
                realLoadImage(imageView, data, imageLoader) {
                    transformations(ZKColiRoundCornersTransform(radius.toFloat(), type))
                    options()
                }
            }
            else -> {
                realLoadImage(imageView, data, imageLoader, options)
            }
        }
    }

    inline  fun realLoadImage(
        imageView: ImageView,
        data: Any?,
        imageLoader: ImageLoader = imageView.context.imageLoader,
        options: ImageRequest.Builder.() -> Unit = {},
    ) = imageView.load(data,imageLoader,options)
}

